/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.Disposition;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.index.CalendarItemHit;
/*     */ import com.zimbra.cs.index.ContactHit;
/*     */ import com.zimbra.cs.index.ConversationHit;
/*     */ import com.zimbra.cs.index.DocumentHit;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.MessagePartHit;
/*     */ import com.zimbra.cs.index.NoteHit;
/*     */ import com.zimbra.cs.index.ProxiedHit;
/*     */ import com.zimbra.cs.index.QueryInfo;
/*     */ import com.zimbra.cs.index.SearchParams;
/*     */ import com.zimbra.cs.index.SearchParams.ExpandResults;
/*     */ import com.zimbra.cs.index.SearchParams.Fetch;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.mailbox.Appointment;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Conversation;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.WikiItem;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.mail.type.ConversationMsgHitInfo;
/*     */ import com.zimbra.soap.mail.type.MessageHitInfo;
/*     */ import com.zimbra.soap.util.JaxbInfo;
/*     */ import java.util.Collection;
/*     */ import java.util.List;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class SearchResponse
/*     */ {
/*  67 */   protected static final Log LOG = LogFactory.getLog(SearchResponse.class);
/*     */   
/*     */   private final ZimbraSoapContext zsc;
/*     */   private final ItemIdFormatter ifmt;
/*     */   private final SearchParams params;
/*     */   private final Element element;
/*     */   private final OperationContext octxt;
/*  74 */   private boolean includeMailbox = false;
/*  75 */   private int size = 0;
/*     */   private final SearchParams.ExpandResults expand;
/*  77 */   private SortBy sortOrder = SortBy.NONE;
/*  78 */   private boolean allRead = false;
/*     */   
/*     */   SearchResponse(ZimbraSoapContext zsc, OperationContext octxt, Element el, SearchParams params) {
/*  81 */     this.zsc = zsc;
/*  82 */     this.params = params;
/*  83 */     this.octxt = octxt;
/*  84 */     this.element = el;
/*  85 */     this.ifmt = new ItemIdFormatter(zsc);
/*  86 */     this.expand = params.getInlineRule();
/*     */   }
/*     */   
/*     */   void setAllRead(boolean value) {
/*  90 */     this.allRead = value;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void setIncludeMailbox(boolean value)
/*     */   {
/*  99 */     this.includeMailbox = value;
/*     */   }
/*     */   
/*     */   void setSortOrder(SortBy value) {
/* 103 */     this.sortOrder = value;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void addHasMore(boolean hasMore)
/*     */   {
/* 112 */     this.element.addAttribute("more", hasMore);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   Element toElement()
/*     */   {
/* 121 */     return this.element;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   int size()
/*     */   {
/* 130 */     return this.size;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void add(ZimbraHit zimbraHit)
/*     */     throws ServiceException
/*     */   {
/* 140 */     add(zimbraHit, false);
/*     */   }
/*     */   
/*     */   void add(ZimbraHit hit, boolean expandMsg)
/*     */     throws ServiceException
/*     */   {
/* 146 */     Element el = null;
/* 147 */     if (this.params.getFetchMode() == SearchParams.Fetch.IDS) {
/* 148 */       if ((hit instanceof ConversationHit))
/*     */       {
/* 150 */         el = this.element.addElement("hit");
/* 151 */         el.addAttribute("id", this.ifmt.formatItemId(hit.getParsedItemID()));
/*     */       } else {
/* 153 */         el = this.element.addElement("hit");
/* 154 */         el.addAttribute("id", this.ifmt.formatItemId(hit.getParsedItemID()));
/*     */       }
/* 156 */     } else { if ((hit instanceof ProxiedHit)) {
/* 157 */         this.element.addElement(((ProxiedHit)hit).getElement().detach());
/* 158 */         this.size += 1;
/* 159 */         return;
/*     */       }
/* 161 */       if ((hit instanceof ConversationHit)) {
/* 162 */         el = add((ConversationHit)hit);
/* 163 */       } else if ((hit instanceof MessageHit)) {
/* 164 */         el = add((MessageHit)hit, expandMsg);
/* 165 */       } else if ((hit instanceof MessagePartHit)) {
/* 166 */         el = add((MessagePartHit)hit);
/* 167 */       } else if ((hit instanceof ContactHit)) {
/* 168 */         el = add((ContactHit)hit);
/* 169 */       } else if ((hit instanceof NoteHit)) {
/* 170 */         el = add((NoteHit)hit);
/* 171 */       } else if ((hit instanceof CalendarItemHit)) {
/* 172 */         el = add((CalendarItemHit)hit);
/* 173 */       } else if ((hit instanceof DocumentHit)) {
/* 174 */         el = add((DocumentHit)hit);
/*     */       } else {
/* 176 */         LOG.error("Got an unknown hit type putting search hits: " + hit);
/* 177 */         return;
/*     */       }
/*     */     }
/*     */     
/* 181 */     if (el != null) {
/* 182 */       this.size += 1;
/* 183 */       el.addAttribute("sf", hit.getSortField(this.sortOrder).toString());
/* 184 */       if (this.includeMailbox) {
/* 185 */         el.addAttribute("id", new ItemId(hit.getAcctIdStr(), hit.getItemId()).toString());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private Element add(ConversationHit hit) throws ServiceException {
/* 191 */     if (this.params.getFetchMode() == SearchParams.Fetch.IDS) {
/* 192 */       Element el = this.element.addNonUniqueElement("c");
/* 193 */       for (MessageHit mhit : hit.getMessageHits()) {
/* 194 */         ConversationMsgHitInfo cMsgHit = new ConversationMsgHitInfo(this.ifmt.formatItemId(mhit.getItemId()));
/* 195 */         cMsgHit.toElement(el);
/*     */       }
/* 197 */       return el;
/*     */     }
/* 199 */     Conversation conv = hit.getConversation();
/* 200 */     MessageHit mhit = hit.getFirstMessageHit();
/* 201 */     Element el = ToXML.encodeConversationSummary(this.element, this.ifmt, this.octxt, conv, mhit == null ? null : mhit.getMessage(), this.params.getWantRecipients());
/*     */     
/*     */ 
/* 204 */     Collection<MessageHit> msgHits = hit.getMessageHits();
/* 205 */     long numMsgs = el.getAttributeLong("n", 0L);
/* 206 */     if ((!this.params.fullConversation()) || (numMsgs == msgHits.size())) {
/* 207 */       for (MessageHit mh : msgHits) {
/* 208 */         Message msg = mh.getMessage();
/* 209 */         doConvMsgHit(el, msg, numMsgs);
/*     */       }
/*     */     } else {
/* 212 */       for (Message msg : conv.getMailbox().getMessagesByConversation(this.octxt, conv.getId(), SortBy.DATE_DESC, -1, false))
/*     */       {
/* 214 */         doConvMsgHit(el, msg, numMsgs);
/*     */       }
/*     */     }
/* 217 */     return el;
/*     */   }
/*     */   
/*     */ 
/*     */   private Element doConvMsgHit(Element el, Message msg, long numMsgsInConv)
/*     */   {
/* 223 */     ConversationMsgHitInfo cMsgHit = ConversationMsgHitInfo.fromIdAndFolderId(this.ifmt.formatItemId(msg), this.ifmt.formatItemId(new ItemId(msg.getMailbox().getAccountId(), msg.getFolderId())));
/*     */     
/*     */ 
/*     */ 
/* 227 */     if (numMsgsInConv == 1L) {
/* 228 */       cMsgHit.setSize(Long.valueOf(msg.getSize()));
/*     */     }
/* 230 */     if ((msg.isDraft()) && (msg.getDraftAutoSendTime() != 0L)) {
/* 231 */       cMsgHit.setAutoSendTime(Long.valueOf(msg.getDraftAutoSendTime()));
/*     */     }
/* 233 */     if (!msg.getFlagString().equalsIgnoreCase("")) {
/* 234 */       cMsgHit.setFlags(msg.getFlagString());
/*     */     }
/* 236 */     cMsgHit.toElement(el);
/* 237 */     return el;
/*     */   }
/*     */   
/*     */   private Element add(MessageHit hit, boolean expandMsg) throws ServiceException
/*     */   {
/* 242 */     Message msg = hit.getMessage();
/*     */     
/* 244 */     if ((expandMsg) && (msg.isUnread()) && (this.params.getMarkRead())) {
/*     */       try
/*     */       {
/* 247 */         msg.getMailbox().alterTag(this.octxt, msg.getId(), msg.getType(), Flag.FlagInfo.UNREAD, false, null);
/*     */       } catch (ServiceException e) {
/* 249 */         if (e.getCode().equals("service.PERM_DENIED")) {
/* 250 */           LOG.info("no permissions to mark message as read (ignored): %d", new Object[] { Integer.valueOf(msg.getId()) });
/*     */         } else {
/* 252 */           LOG.warn("problem marking message as read (ignored): %d", Integer.valueOf(msg.getId()), e);
/*     */         }
/*     */       }
/*     */     }
/*     */     Element el;
/*     */     Element el;
/* 258 */     if (expandMsg) {
/* 259 */       el = ToXML.encodeMessageAsMP(this.element, this.ifmt, this.octxt, msg, null, this.params.getMaxInlinedLength(), this.params.getWantHtml(), this.params.getNeuterImages(), this.params.getInlinedHeaders(), true, this.params.getWantExpandGroupInfo(), LC.mime_encode_missing_blob.booleanValue());
/*     */     }
/*     */     else
/*     */     {
/* 263 */       el = ToXML.encodeMessageSummary(this.element, this.ifmt, this.octxt, msg, this.params.getWantRecipients(), this.params.isQuick() ? 65536 : -4194305);
/*     */     }
/*     */     
/*     */ 
/* 267 */     el.addAttribute("cm", true);
/*     */     
/* 269 */     List<MessagePartHit> parts = hit.getMatchedMimePartNames();
/* 270 */     if (parts != null) {
/* 271 */       for (MessagePartHit mph : parts) {
/* 272 */         String partNameStr = mph.getPartName();
/* 273 */         if (partNameStr.length() > 0) {
/* 274 */           el.addElement("hp").addAttribute("part", partNameStr);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 282 */     JaxbInfo jaxbInfo = JaxbInfo.getFromCache(MessageHitInfo.class);
/* 283 */     List<List<QName>> nameOrder = jaxbInfo.getElementNameOrder();
/* 284 */     return Element.reorderChildElements(el, nameOrder);
/*     */   }
/*     */   
/*     */   private Element add(MessagePartHit hit) throws ServiceException {
/* 288 */     Message msg = hit.getMessageResult().getMessage();
/* 289 */     Element el = this.element.addElement("mp");
/* 290 */     el.addAttribute("s", msg.getSize());
/* 291 */     el.addAttribute("d", msg.getDate());
/* 292 */     el.addAttribute("cid", msg.getConversationId());
/* 293 */     el.addAttribute("mid", msg.getId());
/* 294 */     el.addAttribute("ct", hit.getType());
/* 295 */     el.addAttribute("name", hit.getFilename());
/* 296 */     el.addAttribute("part", hit.getPartName());
/*     */     
/* 298 */     ToXML.encodeEmail(el, msg.getSender(), ToXML.EmailType.FROM);
/* 299 */     String subject = msg.getSubject();
/* 300 */     if (subject != null) {
/* 301 */       el.addAttribute("su", subject, Element.Disposition.CONTENT);
/*     */     }
/*     */     
/* 304 */     return el;
/*     */   }
/*     */   
/*     */   private Element add(ContactHit hit) throws ServiceException {
/* 308 */     return ToXML.encodeContact(this.element, this.ifmt, this.octxt, hit.getContact(), true, null);
/*     */   }
/*     */   
/*     */   private Element add(NoteHit hit) throws ServiceException {
/* 312 */     return ToXML.encodeNote(this.element, this.ifmt, this.octxt, hit.getNote());
/*     */   }
/*     */   
/*     */   private Element add(DocumentHit hit) throws ServiceException {
/* 316 */     if (hit.getItemType() == MailItem.Type.DOCUMENT)
/* 317 */       return ToXML.encodeDocument(this.element, this.ifmt, this.octxt, hit.getDocument());
/* 318 */     if (hit.getItemType() == MailItem.Type.WIKI) {
/* 319 */       return ToXML.encodeWiki(this.element, this.ifmt, this.octxt, (WikiItem)hit.getDocument());
/*     */     }
/* 321 */     throw ServiceException.UNKNOWN_DOCUMENT("invalid document type " + hit.getItemType(), null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Element add(CalendarItemHit hit)
/*     */     throws ServiceException
/*     */   {
/* 332 */     CalendarItem item = hit.getCalendarItem();
/* 333 */     Account acct = DocumentHandler.getRequestedAccount(this.zsc);
/* 334 */     long rangeStart = this.params.getCalItemExpandStart();
/* 335 */     long rangeEnd = this.params.getCalItemExpandEnd();
/* 336 */     if ((rangeStart == -1L) && (rangeEnd == -1L) && ((item instanceof Appointment)))
/*     */     {
/* 338 */       rangeStart = item.getStartTime();
/* 339 */       rangeEnd = rangeStart + 1L;
/*     */     }
/* 341 */     GetCalendarItemSummaries.EncodeCalendarItemResult encoded = GetCalendarItemSummaries.encodeCalendarItemInstances(this.zsc, this.octxt, item, acct, rangeStart, rangeEnd, true);
/*     */     
/*     */ 
/* 344 */     Element el = encoded.element;
/* 345 */     if (el != null) {
/* 346 */       this.element.addElement(el);
/* 347 */       ToXML.setCalendarItemFields(el, this.ifmt, this.octxt, item, -1, false, this.params.getNeuterImages());
/*     */       
/* 349 */       el.addAttribute("cm", true);
/*     */     }
/* 351 */     return el;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void add(List<QueryInfo> qinfo)
/*     */   {
/*     */     Element el;
/*     */     
/* 360 */     if (qinfo.size() > 0) {
/* 361 */       el = this.element.addElement("info");
/* 362 */       for (QueryInfo inf : qinfo) {
/* 363 */         inf.toXml(el);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SearchResponse.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */